package com.example.devinfan.criminalintent.Fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.TimePicker;

import com.example.devinfan.criminalintent.R;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimePickerFragment extends DialogFragment {

    private Date mDate;
    public static final String EXTRA_TIME = "com.example.devinfan.criminalintent.Time";

    @Override
    public Dialog onCreateDialog(Bundle saveInstanceState){
        mDate = (Date)getArguments().getSerializable(EXTRA_TIME);
        Calendar calendar = Calendar.getInstance();
        final int year = calendar.get(Calendar.YEAR);
        final int month = calendar.get(Calendar.MONTH);
        final int day = calendar.get(Calendar.DAY_OF_MONTH);
        final int second = calendar.get(Calendar.SECOND);
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_time,null);

        TimePicker timePicker = (TimePicker)view.findViewById(R.id.dialog_time_datePicker);
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                //Translate hour,day into a date object using a calendar
                mDate = new GregorianCalendar(year,month,day,hourOfDay,minute,second).getTime();
                //Toast.makeText(getActivity(), mDate.toString(), Toast.LENGTH_SHORT).show();
                //update argument to preserve selected value on rotation
                getArguments().putSerializable(EXTRA_TIME,mDate);
            }
        });
        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .setTitle(R.string.date_picker_title)
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //Toast.makeText(getActivity(), mDate.toString(), Toast.LENGTH_SHORT).show();
                                sendResult(Activity.RESULT_OK);
                            }
                        })
                .create();
    }

    public static TimePickerFragment newInstance(Date date){
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_TIME,date);

        TimePickerFragment fragment = new TimePickerFragment();
        fragment.setArguments(args);

        return fragment;
    }

    private void sendResult(int resultCode){
        if (getTargetFragment() == null){
            return ;
        }

        Intent intent = new Intent();
        intent.putExtra(EXTRA_TIME,mDate);

        getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
    }
}
